﻿using System;
using System.Text;
using System.Data;

using LTJ.Common.Generic;
using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;

namespace LTJ.Data.DataProvider
{
    /// <summary>
    /// 在线用户操作
    /// </summary>
    public class OnlineUsers
    {
        /// <summary>
        /// 创建在线表记录(本方法在应用程序初始化时被调用)
        /// </summary>
        /// <returns></returns>
        public static int CreateOnlineTable()
        {
            return DatabaseProvider.GetInstance().CreateOnlineTable();
        }

        public static OnlineUserInfo LoadSingleOnlineUser(IDataReader reader)
        {
            OnlineUserInfo _info = new OnlineUserInfo();
            if (reader.Read())
            {
                _info.id = TypeConverter.StrToInt(reader["id"].ToString());
                _info.uid = TypeConverter.StrToInt(reader["uid"].ToString());
                _info.olip = reader["olip"].ToString();
                _info.lname = reader["lname"].ToString();
                _info.rname = reader["rname"].ToString();
                _info.password = reader["password"].ToString();
                _info.groupid = TypeConverter.StrToInt(reader["groupid"].ToString());
                _info.uimg = reader["uimg"].ToString();
                _info.lastupdate = Utils.GetStandardDateTime(reader["lastupdate"].ToString());
                _info.newpms = TypeConverter.StrToInt(reader["newpms"].ToString());
                _info.verifycode = reader["verifycode"].ToString();
            }
            reader.Close();
            return _info;
        }

        public static OnlineUserInfo LoadSingleOnlineUser(DataRow reader)
        {
            OnlineUserInfo _info = new OnlineUserInfo();
            _info.id = TypeConverter.StrToInt(reader["id"].ToString());
            _info.uid = TypeConverter.StrToInt(reader["uid"].ToString());
            _info.olip = reader["olip"].ToString();
            _info.lname = reader["lname"].ToString();
            _info.rname = reader["rname"].ToString();
            _info.password = reader["password"].ToString();
            _info.groupid = TypeConverter.StrToInt(reader["groupid"].ToString());
            _info.uimg = reader["uimg"].ToString();
            _info.lastupdate = Utils.GetStandardDateTime(reader["lastupdate"].ToString());
            _info.newpms = TypeConverter.StrToInt(reader["newpms"].ToString());
            _info.verifycode = reader["verifycode"].ToString();
            return _info;
        }

        public static OnlineUserInfo GetOnlineUser(int olid)
        {
            return LoadSingleOnlineUser(DatabaseProvider.GetInstance().GetOnlineUserByID(olid));
        }

        /// <summary>
        /// 执行在线用户向表及缓存中添加的操作。
        /// </summary>
        /// <param name="onlineuserinfo">在组用户信息内容</param>
        /// <returns>添加成功则返回刚刚添加的olid,失败则返回0</returns>
        public static int CreateOnlineUserInfo(OnlineUserInfo onlineuserinfo, int timeout)
        {
            return DatabaseProvider.GetInstance().AddOnlineUser(onlineuserinfo, timeout, GeneralConfigs.GetConfig().Deletingexpireduserfrequency);
        }

        // <summary>
        /// 设置用户在线状态
        /// </summary>
        /// <param name="uid">在线用户id</param>
        /// <param name="onlinestate">在线用户状态</param>
        /// <returns></returns>
        public static int SetUserOnlineState(int uid, int onlinestate, string vcode)
        {
            return DatabaseProvider.GetInstance().SetUserOnlineState(uid, 1, vcode);
        }

        /// <summary>
        /// 更新用户IP地址
        /// </summary>
        /// <param name="olid">在线id</param>
        /// <param name="ip">ip地址</param>
        public static void UpdateIP(int olid, string ip)
        {
            DatabaseProvider.GetInstance().UpdateIP(olid, ip);
        }

        /// <summary>
        /// 获得指定用户的详细信息
        /// </summary>
        /// <param name="userid">在线用户ID</param>
        /// <param name="password">用户密码</param>
        /// <returns>用户的详细信息</returns>
        public static OnlineUserInfo GetOnlineUser(int userid, string password)
        {
            OnlineUserInfo onlineuserinfo = null;
            DataTable dt = DatabaseProvider.GetInstance().GetOnlineUser(userid, password);

            if (dt.Rows.Count > 0)
            {
                onlineuserinfo = LoadSingleOnlineUser(dt.Rows[0]);
                dt.Dispose();
            }
            return onlineuserinfo;
        }

        /// <summary>
        /// 获得指定用户的详细信息
        /// </summary>
        /// <returns>用户的详细信息</returns>
        public static OnlineUserInfo GetOnlineUserByIP(int userid, string ip)
        {
            OnlineUserInfo oluser = null;
            DataTable dt = DatabaseProvider.GetInstance().GetOnlineUserByIP(userid, ip);

            if (dt.Rows.Count > 0)
            {
                oluser = LoadSingleOnlineUser(dt.Rows[0]);
                dt.Dispose();
            }
            return oluser;
        }

        /// <summary>
        /// 检查在线用户验证码是否有效
        /// </summary>
        /// <param name="olid">在组用户ID</param>
        /// <param name="verifycode">验证码</param>
        /// <param name="newverifycode">新验证码</param>
        /// <returns>在组用户ID</returns>
        public static bool CheckUserVerifyCode(int olid, string verifycode, string newverifycode)
        {
            return DatabaseProvider.GetInstance().CheckUserVerifyCode(olid, verifycode, newverifycode);
        }

        #region 删除符合条件的用户信息

        /// <summary>
        /// 删除符合条件的一个或多个用户信息
        /// </summary>
        /// <returns>删除行数</returns>
        public static int DeleteRowsByIP(string ip)
        {
            return DatabaseProvider.GetInstance().DeleteRowsByIP(ip);
        }

        /// <summary>
        /// 删除在线表中指定在线id的行
        /// </summary>
        /// <param name="olid">在线id</param>
        /// <returns></returns>
        public static int DeleteRows(int olid)
        {
            return DatabaseProvider.GetInstance().DeleteRows(olid);
        }

        /// <summary>
        /// 更新在线时间
        /// </summary>
        /// <param name="oltimespan">在线时间间隔</param>
        /// <param name="uid">当前用户id</param>
        public static void UpdateOnlineTime(int oltimespan, int uid)
        {
            DatabaseProvider.GetInstance().UpdateOnlineTime(oltimespan, uid);
        }
        /// <summary>
        /// 同步在线时间
        /// </summary>
        /// <param name="uid">用户id</param>
        public static void SynchronizeOnlineTime(int uid)
        {
            DatabaseProvider.GetInstance().SynchronizeOnlineTime(uid);
        }

        /// <summary>
        /// 更新在线操作
        /// </summary>
        public static void UpdateAction(int olid, int actid)
        {
            DatabaseProvider.GetInstance().UpdateAction(olid, actid);
        }
        /// <summary>
        /// 删除指定在线用户信息
        /// </summary>
        /// <param name="uid"></param>
        public static void DeleteRowsByUid(int uid)
        {
            DatabaseProvider.GetInstance().DeleteRowsByUid(uid);
        }
        #endregion
    }
}
